<?php

namespace console\db\channels;

use yii\helpers\Console;
use console\db\Migration;

/**
 * 数据库升级准备
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
abstract class UpgradePrepareMigration extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {

        /**
         * 迁移所需的数据表复制到新数据库
         */
        $requireTables = $this->requiredTransferTable();
        if (!empty($requireTables)) {
            foreach ($requireTables as $table) {
                if (!$this->isExistsTable($table)) {
                    Console::error('Please migrate the original table: ' . $table . ' using the database tool first.');
                    return false;
                }
            }
        }

        /**
         * 创建升级所需的临时表
         */
        foreach ($this->tempTable() as $table) {
            $this->tableBuilder($table);
        }

        /**
         * 升级所需增加的表字段
         */
        foreach ($this->tempAddColumns() as $table => $columns) {
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            $tableColumns = $this->db->getTableSchema($tableName)->columnNames;
            if (empty($columns)) {
                continue;
            }
            foreach ($columns as $column => $type) {
                if (!in_array($column, $tableColumns)) {
                    $this->addColumn($tableName, $column, $type);
                } else {
                    $this->alterColumn($tableName, $column, $type);
                }
            }
        }

        /**
         * 升级所需增加的表索引
         */
        foreach ($this->tempAddIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->createIndex($index, $tableName, $index);
            }
        }

        /**
         * 升级所需临时移除的表索引
         */
        foreach ($this->tempRemoveIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->dropIndex($index, $tableName);
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        
    }

    /**
     * 迁移前所需的数据表
     * @todo 复制所需数据表至新数据库
     * @return array
     */
    abstract public function requiredTransferTable();

    /**
     * 升级所需的临时表
     * @see format use $this->tableBuilder();
     * @return array
     */
    abstract public function tempTable();

    /**
     * 升级所需增加的表字段
     * ```
     * return [
     *      'tablename' => [
     *          'columnName' => 'columnType',
     *      ],
     * ];
     * ```
     * @return array
     */
    abstract public function tempAddColumns();

    /**
     * 升级所需增加的临时索引
     * ```
     * return [
     *      'tablename' => [
     *          'indexcolumnname',
     *          ...
     *      ],
     *      ...
     * ];
     * ```
     * @return array
     */
    abstract public function tempAddIndexs();

    /**
     * 升级所需临时移除的索引
     * ```
     * return [
     *      'tablename' => [
     *          'indexname',
     *          ...
     *      ],
     *      ...
     * ];
     * ```
     * @return array
     */
    abstract public function tempRemoveIndexs();
}
